var myApp = angular.module('myApp', []);
myApp.controller('restApiCtrl', function ($scope, $http, $filter, $httpParamSerializerJQLike) {
  $scope.addUserData = {
    show: false,
    createInfo: false,
  }
  // 权限命令备注
  $scope.cmdList = [
    { name: 'AlertPush', remark: '推送告警以及机器资源消耗情况', check: false },
    { name: 'InfoQuery', remark: '查询机器、状态及各类事件', check: false },
    { name: 'Response', remark: '杀进程、文件黑名单、IP端口黑名单、隔离机器等响应', check: false },
    { name: 'IOCReceiver', remark: '接收第三方推送过来的情报', check: false },
  ]
  // 权限命令备注
  $scope.EditcmdList = [
    { name: 'AlertPush', remark: '推送告警以及机器资源消耗情况', check: false },
    { name: 'InfoQuery', remark: '查询机器、状态及各类事件', check: false },
    { name: 'Response', remark: '杀进程、文件黑名单、IP端口黑名单、隔离机器等响应', check: false },
    { name: 'IOCReceiver', remark: '接收第三方推送过来的情报', check: false },
  ]
  //================
  $scope.search = {
    data: ''
  }
  $scope.pages = {
    data: [],
    count: 0,
    maxPage: "...",
    pageNow: 1,
  };
  // 添加
  $scope.addData = {
    username: "",
    desc: "",
    permission: {
      AlertPush: false,
      InfoQuery: false,
      Response: false,
      IOCReceiver: false
    }
  }
  // 详情数据
  $scope.detailData = []
  $scope.getPage = function (pageNow) {
    pageNow = pageNow ? pageNow : 1;
    let url = `/oauthclient/list?page=${pageNow}&rows=10&search=${$scope.search.data} `
    $http.get(url).then(function success (rsp) {
      console.log(rsp.data);
      if (rsp.data.status == 'success') {
        $scope.pages = rsp.data;
      }
    }, function err (rsp) {
    });
  }
  $scope.getPage();
  //--------
  // 权限弹窗
  $scope.popPower = ((item) => {
    console.log(JSON.parse(item.permission));
    $scope.EditcmdList.map((ele) => {
      // item.
      switch (ele.name) {
        case 'AlertPush':
          ele.check = JSON.parse(item.permission).AlertPush
          break;
        case 'InfoQuery':
          ele.check = JSON.parse(item.permission).InfoQuery
          break;
        case 'Response':
          ele.check = JSON.parse(item.permission).Response
          break;
        case 'IOCReceiver':
          ele.check = JSON.parse(item.permission).IOCReceiver
          break;
        default:
          break;
      }
    })
    // $scope.$apply()
    console.log($scope.EditcmdList);
    var W = 540;
    var H = 380;
    var box = null;
    box = zeroModal.show({
      title: '修改权限',
      content: power,
      width: W + "px",
      height: H + "px",
      ok: true,
      cancel: true,
      okFn: function () {
        console.log(item);
        console.log(111123123);
        let postData = {
          id: item.id,
          permission: {
            AlertPush: false,
            InfoQuery: false,
            Response: false,
            IOCReceiver: false
          }
        }
        $scope.EditcmdList.map((ele) => {
          switch (ele.name) {
            case 'AlertPush':
              postData.permission.AlertPush = ele.check
              break;
            case 'InfoQuery':
              postData.permission.InfoQuery = ele.check
              break;
            case 'Response':
              postData.permission.Response = ele.check
              break;
            case 'IOCReceiver':
              postData.permission.IOCReceiver = ele.check
              break;
            default:
              break;
          }
        })

        $http.post('/oauthclient/edit-permission', postData).then(function success (rsp) {
          console.log(rsp.data);
          if (rsp.data.status == 'success') {
            zeroModal.success('权限修改成功');
            $scope.getPage();
          } else {
            zeroModal.error('权限修改失败');
          }
        }, function err (rsp) {
        });
      },
      onCleanup: function () {
        hideenBox.appendChild(power);
      }
    });
  })
  // 删除
  $scope.del = function (item) {
    zeroModal.confirm({
      content: '操作提示',
      contentDetail: '是否确认删除吗？',
      okFn: function () {
        console.log(item);
        var loading = zeroModal.loading(4);
        $http.get(`/oauthclient/del?id=${item.id}`).then(function success (rsp) {
          zeroModal.close(loading);
          console.log(rsp);
          if (rsp.data.status == 'success') {
            zeroModal.success('删除成功!');
            $scope.getPage();
          }
        }, function err (rsp) {
          zeroModal.close(loading);
        });
      },
      cancelFn: function () {
      }
    });
  }
  // 下载审计日志
  $scope.downloadLog = ((item) => {
    url = `/oauthclient/download-log?id=${item.id}`
    window.location.href = url;
  })
  // 创建用户
  $scope.addUser = (() => {
    if ($scope.addData.username == '') {
      zeroModal.error('请输入用户名');
      return
    }
    console.log($scope.cmdList);
    $scope.cmdList.map((item) => {
      switch (item.name) {
        case 'AlertPush':
          $scope.addData.permission.AlertPush = item.check
          break;
        case 'InfoQuery':
          $scope.addData.permission.InfoQuery = item.check
          break;
        case 'Response':
          $scope.addData.permission.Response = item.check
          break;
        case 'IOCReceiver':
          $scope.addData.permission.IOCReceiver = item.check
          break;

        default:
          break;
      }
    })
    $http.post('/oauthclient/add', $scope.addData).then(function success (rsp) {
      console.log(rsp.data);
      if (rsp.data.status == 'success') {
        $scope.addUserData.createInfo = true
        $scope.UserDetail(rsp.data.data.id)
      } else {
        zeroModal.error('用户添加失败');
      }
    }, function err (rsp) {
    });

  })

  // 创建
  $scope.createUser = (() => {
    $scope.addUserData.show = true
    $scope.cmdList.map((item) => {
      item.check = false
    })
    $scope.addData = {
      username: "",
      desc: "",
      permission: {
        AlertPush: false,
        InfoQuery: false,
        Response: false,
        IOCReceiver: false
      }
    }
  })
  // 查询用户详情
  $scope.UserDetail = ((id) => {
    $http.get('/oauthclient/detail?id=' + id).then(function success (rsp) {
      console.log(rsp.data);
      if (rsp.data.status == 'success') {
        $scope.detailData = []
        $scope.detailData.push(rsp.data.data)
      }
    }, function err (rsp) {
    });

  })
  // 返回用户列表
  $scope.backUserList = (() => {
    $scope.addUserData.show = false
    $scope.addUserData.createInfo = false
    $scope.getPage();
  })
  // 点击复制
  $scope.Copy = ((item) => {
    const el = document.createElement('textarea');
    el.value = `用户名:${item.username}\r\n备注:${item.desc}\r\nClientID:${item.client_id}\r\nClientSecret:${item.client_secret} `;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
    zeroModal.success('复制成功!');
  })
  // 下载Csv
  $scope.DownloadCsv = ((item) => {
    url = `/oauthclient/download-cvs?id=${item.id}`
    window.location.href = url;
  })

});